namespace CSharpPro._1013;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

    public class User
    {
        static Dictionary<string, string> users = new Dictionary<string, string>();

        static void Main1(string[] args)
        {
            Console.WriteLine("===== 用户登录注册系统 =====");
            ShowMainMenu();
        }
        static void ShowMainMenu()
        {
            while (true)
            {
                Console.WriteLine("\n请选择功能：");
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 退出");
                Console.Write("请输入选项(1-3)：");
                string input = Console.ReadLine();
                if (!int.TryParse(input, out int choice))
                {
                    Console.WriteLine("输入错误，请输入有效数字(1-3)！");
                    continue;
                }
                switch (choice)
                {
                    case 1:
                        UserLogin();
                        break;
                    case 2:
                        UserRegister();
                        break;
                    case 3:
                        Console.WriteLine("谢谢使用，再见！");
                        return;
                    default:
                        Console.WriteLine("输入错误，请输入1-3之间的数字！");
                        break;
                }
            }
        }
        static void UserRegister()
        {
            Console.WriteLine("\n===== 用户注册 =====");
            string username;
            while (true)
            {
                Console.Write("请输入用户名：");
                username = Console.ReadLine()?.Trim();
                if (string.IsNullOrEmpty(username))
                {
                    Console.WriteLine("用户名不可为空！");
                    continue;
                }
                if (username.Length < 3 || username.Length > 15)
                {
                    Console.WriteLine("用户名长度必须为3-15个字符！");
                    continue;
                }
                if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
                {
                    Console.WriteLine("用户名只能包含字母、数字和下划线！");
                    continue;
                }
                if (users.ContainsKey(username))
                {
                    Console.WriteLine("该用户名已被注册，请更换其他用户名！");
                    continue;
                }
                break;
            }
            string password;
            while (true)
            {
                Console.Write("请输入密码：");
                password = Console.ReadLine()?.Trim();
                if (string.IsNullOrEmpty(password))
                {
                    Console.WriteLine("密码不可为空！");
                    continue;
                }
                if (password.Length < 6 || password.Length > 20)
                {
                    Console.WriteLine("密码长度必须为6-20个字符！");
                    continue;
                }
                bool hasUpper = Regex.IsMatch(password, @"[A-Z]");
                bool hasLower = Regex.IsMatch(password, @"[a-z]");
                bool hasNumber = Regex.IsMatch(password, @"[0-9]");

                if (!hasUpper || !hasLower || !hasNumber)
                {
                    Console.WriteLine("密码必须包含至少1个大写字母、1个小写字母和1个数字！");
                    continue;
                }
                break;
            }
            while (true)
            {
                Console.Write("请确认密码：");
                string confirmPassword = Console.ReadLine()?.Trim();

                if (string.IsNullOrEmpty(confirmPassword))
                {
                    Console.WriteLine("确认密码不可为空！");
                    continue;
                }

                if (confirmPassword != password)
                {
                    Console.WriteLine("两次输入的密码不一致，请重新输入！");
                    continue;
                }
                break;
            }
            users.Add(username, password);
            Console.WriteLine("注册成功！");
        }
        static void UserLogin()
        {
            Console.WriteLine("\n===== 用户登录 =====");
            Console.Write("请输入用户名：");
            string username = Console.ReadLine()?.Trim();
            if (string.IsNullOrEmpty(username))
            {
                Console.WriteLine("用户名不可为空！");
                return;
            }
            if (!users.ContainsKey(username))
            {
                Console.WriteLine("登录失败：用户名不存在！");
                return;
            }
            Console.Write("请输入密码：");
            string password = Console.ReadLine()?.Trim();
            if (string.IsNullOrEmpty(password))
            {
                Console.WriteLine("密码不可为空！");
                return;
            }
            if (users[username] != password)
            {
                Console.WriteLine("登录失败：密码错误！");
                return;
            }
            Console.WriteLine($"登录成功！欢迎回来，{username}！");
            ShowSystemHomePage(username);
        }
        static void ShowSystemHomePage(string username)
        {
            Console.WriteLine("\n===== 系统首页 =====");
            Console.WriteLine($"您好，{username}！您已成功登录系统。");
            Console.WriteLine("这是一个模拟的系统首页。");
            Console.WriteLine("按任意键返回主菜单...");
            Console.ReadKey();
        }
    }

